<?php

namespace App\Admin\Controllers\portal;

use App\Admin\Repositories\SwtPolicy;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Card;

class SwtPolicyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SwtPolicy(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('content')->display('查看')
                ->modal(function ($modal) {
                    $card = new Card($modal -> row -> content);
                    return <<<HTML
                    <style>
                        .content img {
                            max-width: 100%;
                            height: auto;
                        }
                    </style>
                    <div class="content">{$card->render()}</div>
                HTML;
                });
            $grid->column('sort');
            $grid->column('status') -> switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->showEditButton();
            $grid->createMode('default');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableQuickEdit();
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title') -> width(3);
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', trans('global.options.status'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtPolicy(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtPolicy(), function (Form $form) {
            $form->disableDeleteButton();
            $form->disableViewButton();
            $form->display('id');
            $form->text('title') -> required();
            $form->editor('content') -> required();
            $form->number('sort');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
